import { Share } from "../share";
import { BaseSocketIo } from "./BaseSocketIo";
import {mainWindow} from "@/main";
import { MsgChannel, SocketIoChannel} from "@common/constants";
import {log} from '../logManager';

export class ImageSocketIo extends BaseSocketIo {
  constructor() {
    log.info('socket io init')
    const imageUri = `http://${Share.config!.image.socket_io}/ct`;
    super(imageUri);
    this.listenImageEvents();
    log.info('socket io inited')
  }

  listenImageEvents(): void {
    this.ioInstance.on(SocketIoChannel.Image, (data) => {
      mainWindow.webContents.send(MsgChannel.Image.GET, data)
    });
    this.ioInstance.on(SocketIoChannel.Connect, (data) => {
      console.log('scoketio connect');
      mainWindow.webContents.send(MsgChannel.Connect, data)
    });
    this.ioInstance.on(SocketIoChannel.Disconnect, (data) => {
      console.log('scoketio disconnect');
      mainWindow.webContents.send(MsgChannel.Disconnect, data)
    });
  }

  sendMsg(msg):void {
    // console.log(`发送给image消息：${JSON.stringify(msg)}`);
    this.ioInstance.send(msg)
  }
}
